import moment from 'moment';

export const fistLetterUpper = str => str.charAt(0).toUpperCase() + str.slice(1);

export const getFields = (data, prefix) => {
    if (!data) {
        return {};
    }

    const fields = data.reduce((prev, cur) => {
        const copiedPrev = { ...prev };
        if (typeof cur === 'object' && cur != null) {
            Object.entries(cur).forEach(([key, value]) => {
                let finalValue = value === '' ? undefined : value;
                // 兼容 antd 日期组件数据格式
                if (key.includes('rangeTime')) {
                    finalValue = finalValue.map(item => (item ? moment(item) : undefined));
                }
                if (key.includes('certificateTime')) {
                    finalValue = value ? moment(value) : undefined;
                }
                if (Array.isArray(copiedPrev[`${prefix}${fistLetterUpper(key)}`])) {
                    copiedPrev[`${prefix}${fistLetterUpper(key)}`].push(finalValue);
                } else {
                    copiedPrev[`${prefix}${fistLetterUpper(key)}`] = [finalValue];
                }
            });
            return copiedPrev;
        }
        return prev;
    }, {});

    const finalFields = { ...fields, [`${prefix}StartTime`]: [] };

    const rangeTime = finalFields[`${prefix}RangeTime`] || [];
    finalFields[`${prefix}StartTime`] = rangeTime;

    delete finalFields[`${prefix}EndTime`];
    delete finalFields[`${prefix}RangeTime`];

    return finalFields;
};

export const transformFileList = (files = []) => {
    const fileList = [];
    if (files.length === 0) {
        return {
            allFileList: fileList,
            uploadResourceFile: null
        };
    }
    files.forEach(item => {
        const { originName, url, keyName } = item;
        const finalFile = {
            keyName,
            name: originName,
            url,
            uid: keyName
        };
        fileList.push(finalFile);
    });
    return {
        allFileList: fileList,
        uploadResourceFile: null
    };
};

// 上传公有域文件的回显方法
export const transformPublicFileList = (files = []) => {
    const fileList = [];
    if (files.length === 0) {
        return {
            allFileList: fileList,
            uploadResourceFile: null
        };
    }
    files.forEach(item => {
        const realName = decodeURIComponent(item);
        const start = realName.lastIndexOf('_');
        const finalFile = {
            keyName: item,
            name: realName.substring(start + 1, realName.length),
            url: item,
            uid: item
        };
        fileList.push(finalFile);
    });
    return {
        allFileList: fileList,
        uploadResourceFile: null
    };
};

/**
 * getParse(总录音开始时间,总录音结束时间,片段开始,片段结束), 单位毫秒。
 */
export const getParse = (s1, e1, s2, e2) => {
    // console.log('计算片段时间：', s1, e1, s2, e2);
    if (e2 <= e1) {
        return `${parseInt((s2 - s1) / 1000, 10)},${parseInt((e2 - s1) / 1000, 10)}`;
    }
    if (e2 > e1) {
        if (s2 <= e1) {
            return `${parseInt((s2 - s1) / 1000, 10)},${parseInt((e1 - s1) / 1000, 10)}`;
        }
    }
    return `${parseInt(s1 / 1000, 10)},${parseInt((e1 - s1) / 1000, 10)}`;
};

/**
 * 设置焦点到input的末尾
 */
export const focusInTheEndOfInput = inputRef => {
    if (inputRef.current) {
        console.log(inputRef.current);
        const dom = document.getElementById(`${inputRef.current.props.id}`);
        console.log('focusInTheEndOfInput', dom);
        if (dom) {
            dom.focus();
            dom.setSelectionRange(dom.value.length, dom.value.length);
        }
    }
};
